/**
 * ResultParamConvert.java
 * com.sitech.dmcs.core.param.impl
 * Function： TODO 
 *
 *   ver  author   date    
 * ──────────────────────────────────
 *   	 geutopia  Mar 2, 2011        
 *
 * Copyright (c) 2011, TNT All Rights Reserved.
*/

package com.sitech.timing.param.impl;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;

import com.sitech.timing.QueryService;
import com.sitech.timing.dao.ParamDao;
import com.sitech.timing.dao.TaskDao;
import com.sitech.timing.domain.ParamInfo;
import com.sitech.timing.domain.ParamInstance;
import com.sitech.timing.domain.ParamType;
import com.sitech.timing.domain.TaskInstance;
import com.sitech.timing.param.ParamConvert;
import com.sitech.timing.param.ParamService;


/**
 * ClassName:ResultParamConvert
 * Function: TODO ADD FUNCTION
 * Reason:	 TODO ADD REASON
 *
 * @author   geutopia
 * @version  
 * @since    Ver 1.1
 * @Date	 2011	Mar 2, 2011		3:41:23 PM
 *
 * @see 	 
 */

public class ResultParamConvert  implements ParamConvert 
{
	private ParamDao paramDao;
	private TaskDao taskDao;
	private ParamService paramService;
	private QueryService queryService;


	/**
	 * (non-Javadoc)
	 * @see com.sitech.dmcs.core.param.ParamConvert#Convert(com.sitech.dmcs.core.domain.ParamInfo, com.sitech.dmcs.core.domain.ParamType, com.sitech.dmcs.core.domain.TaskInstance)
	 */
	public Object Convert(ParamInfo paramInfo, ParamType paramType, TaskInstance taskInstance)
	{
		ParamInfo resultParamInfo = queryService.getParamInfo(paramInfo.getParamValue());
		
		TaskInstance resultTaskInstance = taskDao.getTaskInstanceByGroup(taskInstance.getGroupTaskInstanceId(), resultParamInfo.getTaskId());
		ParamInstance queryParamInstance = new ParamInstance();
		queryParamInstance.setParamInfoId(paramInfo.getParamValue());
		queryParamInstance.setTaskInstanceId(resultTaskInstance.getTaskInstanceId());
		List<ParamInstance> paramInstanceList = paramDao.getTaskParamInstance(queryParamInstance);
		
		Object object = paramService.produceParamObject(paramType);
		Map<String, String> paramValueMap = new HashMap<String, String>();

		for(ParamInstance paramInstance : paramInstanceList)
		{
			paramValueMap.put(paramInstance.getParamName(), paramInstance.getParamvalue());
		}
		
		try
		{
			BeanUtils.populate(object, paramValueMap);
		}
		catch (IllegalAccessException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (InvocationTargetException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return object;
	}


	public ParamDao getParamDao()
	{
		return paramDao;
	}


	public void setParamDao(ParamDao paramDao)
	{
		this.paramDao = paramDao;
	}


	public TaskDao getTaskDao()
	{
		return taskDao;
	}


	public void setTaskDao(TaskDao taskDao)
	{
		this.taskDao = taskDao;
	}


	public ParamService getParamService()
	{
		return paramService;
	}


	public void setParamService(ParamService paramService)
	{
		this.paramService = paramService;
	}


	public QueryService getQueryService()
	{
		return queryService;
	}


	public void setQueryService(QueryService queryService)
	{
		this.queryService = queryService;
	}

}
